<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\Request;

class SettingController extends Controller
{
    public function all(Request $request)
    {
        $key = $request->get('key');
        $items = Setting::where('code', $key)->get();
        if (count($items) == 0) {
            $items = Setting::all();
        }
        return jsonResponse(config('common.successCode'), $items);
    }

    public function update(Request $request)
    {
        $key = $request->get('key');
        $value = $request->get('value');

        $setting = Setting::where('code', $key)->first();
        if (empty($setting)) {
            return jsonResponse(40000);
        }
        $setting['value'] = $value;
        $setting->save();
        return jsonResponse();
    }
}
